package com.gc.flashview.adapter;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.gc.flashview.listener.OnItemClickListener;

import java.util.ArrayList;

/**
 * page适配器
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Louis
 * <h3>Date</h3> 2018/7/20 11:11
 * <h3>Copyright</h3> Copyright (c) 2018 Shenzhen ChinaSoft International Co., Ltd. Inc. All rights reserved.
 */
public class MZPagerAdapter extends PagerAdapter{

    /** 是否循环*/
    private boolean loop = true;
    private ArrayList<ImageView> imageViewsList;
    private OnItemClickListener itemClickListener;

    public MZPagerAdapter(ArrayList<ImageView> imageViewsList,boolean loop) {
        this.imageViewsList = imageViewsList;
        this.loop = loop;
    }


    public MZPagerAdapter(ArrayList<ImageView> imageViewsList) {
        this.imageViewsList = imageViewsList;
    }

    public MZPagerAdapter() {

    }


    @Override
    public int getCount() {
        return loop ? Integer.MAX_VALUE : imageViewsList == null ? 0 : imageViewsList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final int realPosition = position % imageViewsList.size();
        View view = imageViewsList.get(realPosition);

        ViewParent vp = view.getParent();
        if (vp != null) {
//            ViewPager pager = (ViewPager) vp;
//            pager.removeView(view);
        }else {
            container.addView(view);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null){
                    itemClickListener.onItemClick(realPosition);
                }
            }
        });
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        int realPosition = position % imageViewsList.size();
        View view = imageViewsList.get(realPosition);
        container.removeView(view);
    }

    /**
     *  测试是否循环轮播
     *  (此处输入方法执行任务.)
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2018/7/20,15:04
     * <h3>UpdateTime</h3> 2018/7/20,15:04
     * <h3>CreateAuthor</h3> Louis
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @param loop
     */
    public void setLoop(boolean loop) {
        this.loop = loop;
        notifyDataSetChanged();
    }

    public int getRealPosition(View view){
        return imageViewsList == null ? getItemPosition(view) : getItemPosition(view) % imageViewsList.size();
    }


    public int getRealCount(){
        return imageViewsList == null ? getCount() : imageViewsList.size();
    }


    public void  setOnItemClickListener(OnItemClickListener listener){
        itemClickListener = listener;
    }

}
